

package yf.mall.service.impl;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.dao.ShopDao;
import yf.mall.entity.ShopEntity;
import  yf.mall.vo.down.ShopParamDown;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import yf.mall.vo.down.ShopParamDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import yf.mall.dao.ShopParamDao;
import yf.mall.entity.ShopParamEntity;
import yf.mall.vo.ShopParamVo;
import yf.mall.service.ShopParamService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Oct 21 16:25:07 CST 2020
 */
@Service
public class ShopParamServiceImpl extends BaseServiceImpl<ShopParamEntity> implements ShopParamService {

    private ShopParamDao dao;


    @Autowired
    public void setDao(ShopParamDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }
    @Autowired
    private ShopDao shopDao;




    @Override
    public List<ShopParamVo> findVo(ShopParamVo vo){
        List<ShopParamVo> voList = dao.findVo(vo);
        if (voList != null && voList.size() > 0){
            for (ShopParamVo shopParamVo : voList) {
                ShopEntity shopEntity = shopDao.get(shopParamVo.getShopParam().getShopId());
                if (shopEntity != null){
                    shopParamVo.getShopParam().setShopName(shopEntity.getName());
                }
            }
        }
        return voList;
    }

    @Override
    public ShopParamVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(ShopParamVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(ShopParamVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<ShopParamVo> list) {
        try {
            List<ShopParamDown> datas = list.stream().map(item -> new ShopParamDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), ShopParamDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMaterialPrice(BigDecimal materialPrice) {
        dao.updateMaterialPrice(materialPrice);
    }


    /**内部方法**/



    /**外部方法实现**/




}
